
package main.nk.mygraph.pane;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import javafx.embed.swing.SwingNode;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;

import java.util.ArrayList;
import java.util.List;

public class MyTabPane extends Pane {

    private TabPane tabPane;

    private double height;

    private double width;

    private Tab[] tabs;

    private SwingNode node;

    private ImageView[] imageView;

    private AnchorPane anchorPane;

    double X = 0;
    double Y = 20;

    //添加图片
    public void addImage(String tableName, Image[] images, final String[] userData) {

        if (images.length != 0) {
            //根据图片的多少来创建view的大小
            imageView = new ImageView[images.length];
            ImageView view;

            this.anchorPane = new AnchorPane();

            if (this.getTabs() != null) {
                for (Tab tab : tabs) {
                    //如果传过来的值等于tab的名称，则向view里面添加图片对象,并将其添加到tab中
                    if (tableName.equals(tab.getText())) {
                        for (int i = 0; i < imageView.length; i++) {
                            //设置图片的大小
                            view = new ImageView(images[i]);
                            view.setFitHeight(40);
                            view.setFitWidth(40);
                            //给每一个view创建点击事件
                            view.setUserData(userData[i]);
                            imageView[i] = view;
                            anchorPane.getChildren().add(view);
                            tab.setContent(anchorPane);

                            //定义图片的位置
                            if (i % 4 != 0) {
                                double point = i % 4;
                                position(point);
                            }
                            if (i % 4 == 0 && (X == 3 * 50)) {
                                //System.out.println(X + "," + i%4);
                                X = 0;
                                Y += 50;
                            }

                            view.setLayoutY(Y);
                            view.setLayoutX(X);

                        }
                    }
                }
            } else {
                throw new NullPointerException("Tabs为空");
            }
        } else {
            throw new NullPointerException("images为空");
        }
    }

    //计算坐标值
    public void position(double point) {
        X = point * 50;
        System.out.println(X);
    }

    public MyTabPane(TabPane tabPane, double height, double width, String[] names, boolean close) {

        //根据名称的长度来设置选项卡的大小,如果为空，则抛出空指针异常
        if (names.length != 0) {
            Tab[] tabs = new Tab[names.length];
            for (int i = 0; i < names.length; i++) {
                tabs[i] = new Tab(names[i]);
            }
            setTabs(tabs);
            tabPane.getTabs().addAll(tabs);
            //更具是否想要有关闭按钮来设置是否能够关闭
            if (!close) {
                for (Tab tab : tabs) {
                    tab.setClosable(true);
                }
            } else {
                for (Tab tab : tabs) {
                    tab.setClosable(false);
                }
            }

            tabPane.setPrefSize(width, height);

            tabPane.setStyle("-fx-border-color: bisque;");
            setTabPane(tabPane);
            setMyHeight(height);
            setMyWidth(width);
        } else {
            throw new NullPointerException("names为空");
        }

    }


    public MyTabPane() {

    }


    public TabPane getTabPane() {
        return tabPane;
    }

    public void setTabPane(TabPane tabPane) {
        this.tabPane = tabPane;
    }


    public double getMyHeight() {
        return height;
    }

    public void setMyHeight(double height) {
        this.height = height;
    }

    public double getMyWidth() {
        return width;
    }

    public void setMyWidth(double width) {
        this.width = width;
    }

    public Tab[] getTabs() {
        return tabs;
    }

    public void setTabs(Tab[] tabs) {
        this.tabs = tabs;
    }

    public SwingNode getNode() {
        return node;
    }

    public void setNode(SwingNode node) {
        this.node = node;
    }

    public AnchorPane getAnchorPane() {
        return anchorPane;
    }

    public void setAnchorPane(AnchorPane anchorPane) {
        this.anchorPane = anchorPane;
    }

    public ImageView[] getImageView() {
        return imageView;
    }

    public void setImageView(ImageView[] imageView) {
        this.imageView = imageView;
    }
}

